package 笔试.科大讯飞;

//记录包含26个字母的最小字符串长度，如"abcdefghijklmnopqrstuvw???xyz"
public class Demo2 {
    public static void main(String[] args) {
        String s = "abcdefghijklmnopqrstuvwwwwxyz";
        System.out.println(findInterval(s));
    }

    public static int findInterval (String str) {
        // write code here
        int res = Integer.MAX_VALUE;
        //数组记录小写字母
        int[] count = new int[26];
        int n = str.length();
        int left = 0;
        int right = 0;
        //记录'？'个数
        int symbolCount = 0;
        //记录字母的个数
        int letterCount = 0;
        while (right < n) {
            //扩大窗口
            char ch = str.charAt(right++);
            if (ch == '?') {
                symbolCount++;
            } else {
                int index = ch - 'a';
                if (count[index] == 0) {
                    letterCount++;
                }
                count[index]++;
            }
            if (symbolCount + letterCount == 26) {
                while (left < right) {
                    char ch1 = str.charAt(left);
                    if (ch1 == '?') {
                        break;
                    } else {
                        //缩小窗口
                        if (count[ch1 - 'a'] > 1) {
                            count[ch1 - 'a']--;
                            left++;
                        } else {
                            break;
                        }
                    }
                }
                res = Math.min(res, right - left); //记录最小长度
                // 继续缩小
                char ch2 = str.charAt(left);
                if (ch2 == '?') {
                    symbolCount--;
                } else {
                    count[ch2 - 'a']--;
                    letterCount--;
                }
                left++;
            }


        }
        return res == Integer.MAX_VALUE ? -1 : res;
    }
}
